﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Resn.WhuPub.BLL;
using System.Collections.ObjectModel;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// ManageExamineePage.xaml 的交互逻辑
        /// </summary>
        public partial class ManageBranchsPage : Page
        {
                private readonly GlobalManager _gm = GlobalManager.Instance;

                public ManageBranchsPage()
                {
                        InitializeComponent();
                }

                private void Page_Loaded(object sender, RoutedEventArgs e)
                {
                        if (_gm.LoginEmployee == null)
                        {
                                MessageBox.Show("请先登录系统");
                                NavigationService.Navigate(new Uri("StartPage.xaml", UriKind.Relative));
                        }
                        LayoutRoot.DataContext = _gm;
                }

                private void CreateButton_Click(object sender, RoutedEventArgs e)
                {
                        if (_gm.Corporate == null)
                        {
                                MessageBox.Show("请选择要新建校区的学校");
                                _gm.Corporates = new ObservableCollection<Corporate>(_gm.LoginEmployee.Corporates);
                                NavigationService.Navigate(new Uri("ManageCorporatesPage.xaml", UriKind.Relative));
                        }
                        else
                        {
                                new CreateBranchWindow().ShowDialog();
                        }
                        
                }

                private void UpdateButton_Click(object sender, RoutedEventArgs e)
                {
                        if (_gm.Branch == null)
                        {
                                MessageBox.Show("请选择要编辑的校区");
                                return;
                        }
                        new UpdateBranchWindow().ShowDialog();
                }

                private void DeleteButton_Click(object sender, RoutedEventArgs e)
                {
                        // 在此处新建事件处理程序实现。
                        if (_gm.Branch == null)
                        {
                                MessageBox.Show("请选择要删除的学校");
                                return;
                        }

                        if (
                                MessageBox.Show("确定要删除" + _gm.Branch.Name + "及其所属校区吗？",
                                                "警告", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                        {
                                _gm.Branch.Delete();
                                _gm.Branchs.Remove(_gm.Branch);
                                _gm.Branch = null;
                        }
                }

                private void SearchButton_Click(object sender, RoutedEventArgs e)
                {
                        new SearchBranchWindow().ShowDialog();
                }

                private void BranchsListBox_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
                {
                        if (_gm.Branch == null)
                        {
                                MessageBox.Show("请选择要查看部门的校区");
                                return;
                        }

                        _gm.Departments = new ObservableCollection<Department>(_gm.Branch.Departments);
                        NavigationService.Navigate(new Uri("ManageDepartmentsPage.xaml", UriKind.Relative));
                }

                
                
        }
}